package com.busi.wms.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.busi.wms.model.WmsLocation;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.zcurd.common.tool.DBTool;
import com.zcurd.common.util.StringUtil;
import com.zcurd.controller.BaseController;
import com.zcurd.ext.render.csv.CsvRender;

/**
 * 库位管理
 */
public class WmsLocationController extends BaseController {
	
	public void listPage() {
		setAttr("dictDatastorage_no", WmsLocation.me.getDictDatastorage_no());
		setAttr("dictDataLoc_type", WmsLocation.me.getDictDataLoc_type());
		setAttr("dictDatais_use", WmsLocation.me.getDictDatais_use());
		render("list.html");
	}
	
	public void listData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id asc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_location", properties, symbols, values, orderBy, getPager());
		Map<String, Object> dictDatastorage_no = WmsLocation.me.getDictDatastorage_no();
		for(Record record : list) {
			String fieldName = "storage_no";
			if(dictDatastorage_no.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatastorage_no.get(record.get(fieldName).toString()));
			}
		}
		
		renderDatagrid(
			list, 
			DBTool.countByMultProperties("wms_location", properties, symbols, values)
		);
	}
	
	//增加页面
	public void addPage() {
		setAttr("dictDatastorage_no", WmsLocation.me.getDictDatastorage_no());
		setAttr("dictDataLoc_type", WmsLocation.me.getDictDataLoc_type());
		setAttr("dictDatais_use", WmsLocation.me.getDictDatais_use());
		render("add.html");
	}
	
	//增加
	public void add() {
		getModel(WmsLocation.class, "model").save();
		
		addOpLog("[库位管理] 增加");
		renderSuccess();
	}
	
	//修改页面
	public void updatePage() {
		setAttr("dictDatastorage_no", WmsLocation.me.getDictDatastorage_no());
		setAttr("dictDataLoc_type", WmsLocation.me.getDictDataLoc_type());
		setAttr("dictDatais_use", WmsLocation.me.getDictDatais_use());
		setAttr("model", WmsLocation.me.findById(getPara("id")));
		render("update.html");
	}
	
	//修改
	public void update() {
		WmsLocation model = WmsLocation.me.findById(getPara("id"));
		model.set("loc_no", getPara("model.loc_no"));
		model.set("loc_barcode", getPara("model.loc_barcode"));
		model.set("loc_name", getPara("model.loc_name"));
		model.set("storage_no", getPara("model.storage_no"));
		model.set("Loc_type", getPara("model.Loc_type"));
		model.set("length", getPara("model.length"));
		model.set("width", getPara("model.width"));
		model.set("height", getPara("model.height"));
		model.set("x", getPara("model.x"));
		model.set("y", getPara("model.y"));
		model.set("z", getPara("model.z"));
		model.set("is_use", getPara("model.is_use"));
		model.set("tp_tnum", getPara("model.tp_tnum"));
		model.set("tp_cnum", getPara("model.tp_cnum"));
		model.update();
		addOpLog("[库位管理] 修改");
		renderSuccess();
	}
	
	//删除
	public void delete() {
		Integer[] ids = getParaValuesToInt("id[]");
		for (Integer id : ids) {
			new WmsLocation().set("id", id).delete();
			
		}
		
		addOpLog("[库位管理] 删除");
		renderSuccess();
	}
	
	//详情页面
	public void detailPage() {
		WmsLocation model = WmsLocation.me.findById(getParaToInt("id"));
		Map<String, Object> dictDatastorage_no = WmsLocation.me.getDictDatastorage_no();
		if(dictDatastorage_no.get(model.get("storage_no").toString()) != null) {
			model.set("storage_no", dictDatastorage_no.get(model.get("storage_no").toString()));
		}
		Map<String, Object> dictDataLoc_type = WmsLocation.me.getDictDataLoc_type();
		if(dictDataLoc_type.get(model.get("Loc_type").toString()) != null) {
			model.set("Loc_type", dictDataLoc_type.get(model.get("Loc_type").toString()));
		}
		
		Map<String, Object> dictDatais_use = WmsLocation.me.getDictDatais_use();
		if(dictDatais_use.get(model.get("is_use").toString()) != null) {
			model.set("is_use", dictDatais_use.get(model.get("is_use").toString()));
		}
		setAttr("model", model);
		render("detail.html");
	}
	
	//导出csv
	public void exportCsv() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_location", properties, symbols, values);
		Map<String, Object> dictDatastorage_no = WmsLocation.me.getDictDatastorage_no();
		for(Record record : list) {
			String fieldName = "storage_no";
			if(dictDatastorage_no.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatastorage_no.get(record.get(fieldName).toString()));
			}
		}
		
		List<String> headers = new ArrayList<String>();
		List<String> clomuns = new ArrayList<String>();
		headers.add("库位编号");
		clomuns.add("loc_no");
		headers.add("条码");
		clomuns.add("loc_barcode");
		headers.add("库位名称");
		clomuns.add("loc_name");
		headers.add("仓库编号");
		clomuns.add("storage_no");
		headers.add("库位类型");
		clomuns.add("Loc_type");
		headers.add("长");
		clomuns.add("length");
		headers.add("宽");
		clomuns.add("width");
		headers.add("高");
		clomuns.add("height");
		headers.add("X坐标");
		clomuns.add("x");
		headers.add("Y坐标");
		clomuns.add("y");
		headers.add("Z坐标");
		clomuns.add("z");
		headers.add("是否在用");
		clomuns.add("is_use");
		headers.add("托盘格子数");
		clomuns.add("tp_tnum");
		headers.add("使用格子数");
		clomuns.add("tp_cnum");
		
		CsvRender csvRender = new CsvRender(headers, list);
		csvRender.clomuns(clomuns);
		csvRender.fileName("库位管理");
		
		addOpLog("[库位管理] 导出cvs");
		render(csvRender);
	}
	/**
	 * 获取所有仓位列表及编号
	 */
	public void getLocList(){
		 
		 renderDatagrid(Db.find("select id,loc_name from wms_location"));
	}
	/**
	 * 获取所有仓库列表及编号
	 */
	public void getStoreList(){
		
		renderDatagrid(Db.find("select id,storage_name from wms_storage"));
	}
}
